package com.ihr360.job.core.repeat.support;

import com.ihr360.job.core.repeat.RepeatContext;

/**
 * Global variable support for repeat clients. Normally it is not necessary for
 * clients to be aware of the surrounding environment because a
 * {@link RepeatCallback} can always use the context it is passed by the
 * enclosing {@link RepeatOperations}. But occasionally it might be helpful to
 * have lower level access to the ongoing {@link RepeatContext} so we provide a
 * global accessor here. The mutator methods ({@link #clear()} and
 * {@link #register(RepeatContext)} should not be used except internally by
 * {@link RepeatOperations} implementations.
 *
 * @author Dave Syer
 *
 */
public final class RepeatSynchronizationManager {

    private static final ThreadLocal<RepeatContext> contextHolder = new ThreadLocal<RepeatContext>();

    private RepeatSynchronizationManager() {
    }

    /**
     * Getter for the current context. A context is shared by all items in the
     * batch, so this method is intended to return the same context object
     * independent of whether the callback is running synchronously or
     * asynchronously with the surrounding {@link RepeatOperations}.
     *
     * @return the current {@link RepeatContext} or null if there is none (if we
     * are not in a batch).
     */
    public static RepeatContext getContext() {
        return contextHolder.get();
    }

    /**
     * Convenience method to set the current repeat operation to complete if it
     * exists.
     */
    public static void setCompleteOnly() {
        RepeatContext context = getContext();
        if (context != null) {
            context.setCompleteOnly();
        }
    }

    /**
     * Method for registering a context - should only be used by
     * {@link RepeatOperations} implementations to ensure that
     * {@link #getContext()} always returns the correct value.
     *
     * @param context a new context at the start of a batch.
     * @return the old value if there was one.
     */
    public static RepeatContext register(RepeatContext context) {
        RepeatContext oldSession = getContext();
        RepeatSynchronizationManager.contextHolder.set(context);
        return oldSession;
    }

    /**
     * Clear the current context at the end of a batch - should only be used by
     * {@link RepeatOperations} implementations.
     *
     * @return the old value if there was one.
     */
    public static RepeatContext clear() {
        RepeatContext context = getContext();
        RepeatSynchronizationManager.contextHolder.set(null);
        return context;
    }

    /**
     * Set current session and all ancestors (via parent) to complete.,
     */
    public static void setAncestorsCompleteOnly() {
        RepeatContext context = getContext();
        while (context != null) {
            context.setCompleteOnly();
            context = context.getParent();
        }
    }

}
